package com.lagou.springbootjob.controller;

import com.lagou.springbootjob.bean.BlogArticle;
import com.lagou.springbootjob.service.BlogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("blog")
public class BlogController {

    @Autowired
    private BlogService blogService;

    @RequestMapping("index")
    public String browser(Model model){

        return "blog_index";
    }

    @RequestMapping("toDistPage")
    public String toDistPage(Model model, int pageIndex){

        Pageable pageable = PageRequest.of(pageIndex, 3);
        Page<BlogArticle> articlePage = blogService.findAll(pageable);

        int curPageIndex = pageIndex;
        int previousPageIndex = curPageIndex - 1;
        int nextPageIndex = curPageIndex + 1;
        int lastPageIndex = articlePage.getTotalPages()-1;


        model.addAttribute("curPageIndex", curPageIndex);
        model.addAttribute("previousPageIndex", previousPageIndex);
        model.addAttribute("nextPageIndex", nextPageIndex);
        model.addAttribute("lastPageIndex", lastPageIndex);
        model.addAttribute("articleList", articlePage.getContent());

        return "client/index";
    }
}
